package com.atguigu.gmall.product.config;

import io.minio.MinioClient;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;


@ConfigurationProperties(prefix = "minio")
@Configuration
@Component
@Data
public class MinioConfig {
    /**
     *   url: https://192.168.200.188:9000
     *   accessKey: gmall-oss
     *   secretKey: gmall123456
     *   secure: false
     *   defaultBucket: gmall-oss
     */

    private String url;
    private String accessKey;
    private String secretKey;
    private String defaultBucket;



    @Bean
    public MinioClient minioClient() {
        try {
            MinioClient minioClient = new MinioClient(url, accessKey, secretKey);
            boolean isExists = minioClient.bucketExists(defaultBucket);
            if (!isExists) {
                minioClient.makeBucket(defaultBucket);
            }
            return minioClient;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
